﻿using System;

namespace KuiHuaBaoDian.Services.Battle.Entity {

    /// <summary>
    /// 战斗实体实例ID
    /// </summary>
    public readonly struct BattleEntityInstanceID {

        private static uint COUNT;

        /// <summary>
        /// 空值
        /// </summary>
        public static readonly BattleEntityInstanceID Empty = new() { ID = BattleEntityID.Empty, GUID = new Guid() };

        /// <summary>
        /// ID
        /// </summary>
        public BattleEntityID ID { get; init; }

        private readonly uint m_Index;

        /// <summary>
        /// 生产索引号
        /// </summary>
        public uint Index {
            get => m_Index;
            init => m_Index = COUNT++;
        }

        private readonly Guid m_GUID;
        private readonly int m_HashCode;

        /// <summary>
        /// GUID
        /// </summary>
        public Guid GUID {
            get => m_GUID;
            init {
                m_GUID = value;
                m_HashCode = m_GUID.GetHashCode();
            }
        }

        public override int GetHashCode() => m_HashCode;

        public override readonly bool Equals(object obj) {
            if (obj is BattleEntityInstanceID other) {
                return other.m_GUID == m_GUID;
            }
            return false;
        }

        public override string ToString() => $"[{nameof(BattleEntityInstanceID)}({nameof(ID.Type)}={ID.Type},{nameof(ID.SupplementID)}={ID.SupplementID},{nameof(GUID)}={m_GUID})]";

        public static bool operator ==(BattleEntityInstanceID left, BattleEntityInstanceID right) => left.Equals(right);

        public static bool operator !=(BattleEntityInstanceID left, BattleEntityInstanceID right) => !left.Equals(right);
    }
}
